package com.putao.album.tag.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.R;
import com.putao.album.base.PtBaseAdapter;
import com.putao.album.tag.bean.TagAlbumPhotoItem;
import com.putao.album.util.BitmapHelper;
import com.putao.album.util.StringHelper;
import com.putao.album.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jidongdong on 15/4/29.
 */
public class TagAlbumPhotoAddAdapter extends PtBaseAdapter implements StickyGridHeadersSimpleAdapter {
    private LayoutInflater mInflater;
    private GridView mGridView;
    private OnHeaderSelectButtonClickedListener mOnHeaderSelectButtonClickedListener;

    public TagAlbumPhotoAddAdapter(Context context, List<TagAlbumPhotoItem> datalist, GridView gridView) {
        super(context, datalist);
        mInflater = LayoutInflater.from(context);
        mGridView = gridView;
    }


    @Override
    public long getHeaderId(int position) {
        return ((TagAlbumPhotoItem) mDatalist.get(position)).section;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder mHeaderHolder;
        if (convertView == null) {
            mHeaderHolder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.activity_tag_album_photo_add_header, parent, false);
            mHeaderHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            mHeaderHolder.tv_button = (TextView) convertView.findViewById(R.id.tv_button);
            convertView.setTag(mHeaderHolder);
        } else {
            mHeaderHolder = (HeaderViewHolder) convertView.getTag();
        }
        TagAlbumPhotoItem item = ((TagAlbumPhotoItem) mDatalist.get(position));
        String date_str = item.photo_datetime;
        if (!StringHelper.isEmpty(date_str)) {
            // int month = DateUtil.getMonth(DateUtil.getDate(date_str));
            // int day = DateUtil.getDay(DateUtil.getDate(date_str));
//            String week = DateUtil.getWeekSting(DateUtil.getDate(date_str));

            String curdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
            if (date_str.startsWith(curdate)) {
                mHeaderHolder.tv_title.setText("今天");
            } else {
                mHeaderHolder.tv_title.setText(date_str);
            }
        }
        mHeaderHolder.tv_button.setTag(item.section + "");
        mHeaderHolder.tv_button.setText(item.section_selected ? "取消全选" : "全选");
        if (mOnHeaderSelectButtonClickedListener != null) {
            mHeaderHolder.tv_button.setOnClickListener(mOnHeaderSelectButtonClickedListener);
        }
        return convertView;
    }

    public void setOnHeaderSelectButtonClickedListener(OnHeaderSelectButtonClickedListener listener) {
        mOnHeaderSelectButtonClickedListener = listener;
    }

    public interface OnHeaderSelectButtonClickedListener extends View.OnClickListener {

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activity_tag_album_photo_add_item, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.btn_select_icon = (ImageView) convertView.findViewById(R.id.btn_select_icon);
            viewHolder.frame_add_flag = (FrameLayout) convertView.findViewById(R.id.frame_add_flag);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TagAlbumPhotoItem item = (TagAlbumPhotoItem) getItem(position);
        if (!StringHelper.isEmpty(item.image_url)) {
            DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(BitmapHelper.getLoadingDrawable())
                    .showImageOnFail(BitmapHelper.getLoadingDrawable()).showImageForEmptyUri(BitmapHelper.getLoadingDrawable())
                    .considerExifParams(true).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
            ImageLoader.getInstance().displayImage(item.image_url, viewHolder.imageView, options);
        }
        viewHolder.btn_select_icon.setVisibility(View.GONE);
        viewHolder.frame_add_flag.setVisibility(View.INVISIBLE);
        if (item.canAddTag) {
            viewHolder.btn_select_icon.setVisibility(View.VISIBLE);
            if (item.selected) {
                viewHolder.btn_select_icon.setBackgroundResource(R.drawable.album_button_choose);
            } else {
                viewHolder.btn_select_icon.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
            }
        } else {
            viewHolder.frame_add_flag.setVisibility(View.VISIBLE);
        }
//        if (item.section_selected) {
//            viewHolder.btn_select_icon.setBackgroundResource(R.drawable.album_button_choose);
//        }
        return convertView;
    }

    class ViewHolder {
        public ImageView imageView;
        public ImageView btn_select_icon;
        public FrameLayout frame_add_flag;

    }

    class HeaderViewHolder {
        public TextView tv_title;
        public TextView tv_button;
    }
}
